import { bindPhone, getSmS, smsCountry } from "../../../api.js"
Page({
  data: {
    phone: "",
    code: "",
    btn: {
      text: "获取验证码",
      disabled: false,
    },
    agree: true,
    smsCountry_lis: [],
    index: -1
  },
  handleBindPhone() {
    let xp = this
    let { phone, code, agree } = xp.data
    if (!phone || !code) {
      wx.showToast({
        title: "手机号及验证码必须填写",
        duration: 1000,
        icon: "none"
      })
      return
    }

    if (!agree) {
      wx.showToast({
        title: "您还没有同意<<芮享用户绑定手机协议>>",
        duration: 1000,
        icon: "none"
      })
      return
    }
    let openid = getApp().globalData.Store.user.openid;
    let businessId = getApp().globalData.Store.business.id;

    bindPhone(phone, code, openid, businessId).then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      // 绑定成功信息更新 回退页面
      if (res.data.code === 666) {
        getApp().globalData.Store.user.phone = phone
        getApp().globalData.Store.token = res.data.data.token
        wx.navigateBack()
      }
    })
  },
  syncData(e) {
    let { key } = e.target.dataset
    let val = e.detail.value
    this.setData({
      [key]: val
    })
  },
  handleCodeTap() {
    if (!this.data.phone) {
      wx.showToast({
        title: "手机号不能为空",
        duration: 1000,
        icon: "none"
      })
      return false
    }
    if (this.data.btn.disable) return;
    if (this.data.index == -1 || this.data.smsCountry_lis[this.data.index].phonecode == 86) {
      getSmS(this.data.phone).then(res => {
        if (res.data.code == 666) {
          this.disableBtn();
        }
        wx.showToast({
          title: res.data.msg,
          duration: 1000,
          icon: "none"
        })
      })
    } else {
      var num = this.data.smsCountry_lis[this.data.index].phonecode + this.data.phone
      getSmS(num).then(res => {
        if (res.data.code == 666) {
          this.disableBtn();
        }
        wx.showToast({
          title: res.data.msg,
          duration: 1000,
          icon: "none"
        })
      })
    }
  },
  disableBtn(sec = 60) {
    let xp = this;
    this.setData({
      btn: {
        text: sec + "s",
        disable: true
      }
    })

    function increaseSec() {
      let timeout = setTimeout(() => {
        if (sec > 0) {
          sec--
          xp.setData({
            btn: {
              text: sec + "s",
              disable: true
            }
          })
          clearTimeout(timeout)
          increaseSec()
        } else {
          clearTimeout(timeout)
          xp.setData({
            btn: {
              text: "获取验证码",
              disable: false
            }
          })
        }
      }, 1000)
    }
    increaseSec()
  },
  handleCheck(e) {
    this.setData({
      agree: !this.data.agree
    })
  },
  onLoad() {
    var that = this
    smsCountry().then(res => {
      console.log(res)
      if (res.data.code == 666) {
        console.log(res.data.data)
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].net = res.data.data[i].name_zh
        }
        console.log(res.data.data)
        that.setData({
          smsCountry_lis: res.data.data
        })
      }
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  }
})